/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;


import dao.QuyDinhDAO;
import java.util.List;
import javax.swing.JOptionPane;
import pojo.QuyDinh;
/**
 *
 * @author BuiThiKieu
 */
public class QuyDinhGUI extends javax.swing.JPanel {

    private JErrorProvider err;
    
    /**
     * Creates new form QuyDinhGUI
     */
    public QuyDinhGUI() throws Exception {
        initComponents();
        AutoLoad();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCapNhat = new javax.swing.JButton();
        txtSoLuongNhapToiDa = new javax.swing.JTextField();
        txtSoLuongNhapToiThieu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();

        btnCapNhat.setText("Cập nhật");
        btnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapNhatActionPerformed(evt);
            }
        });

        txtSoLuongNhapToiDa.setPreferredSize(new java.awt.Dimension(100, 20));

        txtSoLuongNhapToiThieu.setPreferredSize(new java.awt.Dimension(100, 20));

        jLabel1.setText("Số lượng nhập tối thiểu");
        jLabel1.setPreferredSize(new java.awt.Dimension(120, 20));

        jLabel2.setText("Số lượng nhập tối đa");
        jLabel2.setPreferredSize(new java.awt.Dimension(120, 20));

        jLabel3.setText("ID");

        txtID.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addComponent(btnCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(121, 121, 121))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtSoLuongNhapToiThieu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtSoLuongNhapToiDa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtID)))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoLuongNhapToiThieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoLuongNhapToiDa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addComponent(btnCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(35, 35, 35))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(309, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(90, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private int CheckBeforUpdate(){
     
        //err.clear();
        int dem = 0;
        if(txtSoLuongNhapToiThieu.getText().trim().isEmpty() )
        {
            dem++;
            //err.setError(txtSoLuongNhapToiThieu, "Không được để trống");
        }
        if(txtSoLuongNhapToiDa.getText().trim().isEmpty() )
        {
            dem++;
           // err.setError(txtSoLuongNhapToiDa, "Không được để trống");
        }
        //else{
        //    int toiThieu = Integer.parseInt(txtSoLuongNhapToiThieu.getText().toString());
        //    int toiDa = Integer.parseInt(txtSoLuongNhapToiDa.getText().toString());
        //    if(toiThieu >= toiDa){
         //   dem++;
         //   }
       // }
        return dem;
    }
    private void btnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapNhatActionPerformed
        if(CheckBeforUpdate() > 0){
            JOptionPane.showMessageDialog(this, "Vui lòng kiểm tra lại thông tin");
        }
        else{
            List<QuyDinh> ds = QuyDinhDAO.LayDanhSachQuyDinhByID(txtID.getText().toString());
            for(int i = 0; i < ds.size(); i++){
                QuyDinh quyDinh = ds.get(i);
                int toiThieu = Integer.parseInt(txtSoLuongNhapToiThieu.getText().toString());
                int toiDa = Integer.parseInt(txtSoLuongNhapToiDa.getText().toString());
                quyDinh.setSoLuongHangNhapToiThieu(toiThieu);
                quyDinh.setSoLuongHangNhapToiDa(toiDa);
                boolean kq = QuyDinhDAO.CapNhatQuyDinh(quyDinh);
                if(kq == false){
                    JOptionPane.showMessageDialog(this, "Cập nhật không thành công");
                }
                else{
                    JOptionPane.showMessageDialog(this, "Cập nhật thành công");   
                }
            }
        }
    }//GEN-LAST:event_btnCapNhatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCapNhat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtSoLuongNhapToiDa;
    private javax.swing.JTextField txtSoLuongNhapToiThieu;
    // End of variables declaration//GEN-END:variables
 
    private void AutoLoad() throws Exception {
          List<QuyDinh> ds = QuyDinhDAO.LayDanhSachQuyDinh();
          for(int i = 0; i < ds.size(); i++){
              QuyDinh quyDinh = ds.get(i);
              String id =String.valueOf(quyDinh.getId());
              String toiThieu =String.valueOf(quyDinh.getSoLuongHangNhapToiThieu());
              String toiDa =String.valueOf(quyDinh.getSoLuongHangNhapToiDa());
              
              txtID.setText(id);
              txtSoLuongNhapToiThieu.setText(toiThieu);
              txtSoLuongNhapToiDa.setText(toiDa);
          }
    }
}


